package org.ocean.general.sample.conditional;

import org.ocean.general.sample.constant.OptionalConstant;
import org.ocean.general.sample.enums.SortEnum;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class Sqls {
    private List<Creator> sqls = new ArrayList<>();
    private CreatorSort creatorSort;
    public Sqls() {

    }
    public static Sqls build(){
        return  new Sqls();
    }
    public Sqls sort(Iterable<String> keys,SortEnum condition,String... alias){
        if(Objects.nonNull(keys)) {
            CreatorSort creator = new CreatorSort();
            creator.setKeys(keys);
            creator.setCondition(condition);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.creatorSort =creator;
        }
        return this;
    }

    public Sqls andEqualTo(String key,Object value,String... alias){
        if(Objects.nonNull(value)) {
            Creator creator = new Creator();
            creator.setKey(key);
            creator.setValue(Collections.singletonList(value));
            creator.setCondition(OptionalConstant.AND);
            creator.setCrucialState(OptionalConstant.EQUAL);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls andNotEqualTo(String key,Object value,String... alias){
        if(Objects.nonNull(value)) {
            Creator creator = new Creator();
            creator.setKey(key);
            creator.setValue(Collections.singletonList(value));
            creator.setCondition(OptionalConstant.AND);
            creator.setCrucialState(OptionalConstant.NOT_EQUAL);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls andIn(String key,Iterable<?> iterable,String... alias){
        if(Objects.nonNull(iterable)) {
            Creator creator = new Creator();
            creator.setKey(key);
            creator.setValue(iterable);
            creator.setCondition(OptionalConstant.AND);
            creator.setCrucialState(OptionalConstant.IN);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls andNotIn(String key,Iterable<?> iterable,String... alias){
        if(Objects.nonNull(iterable)) {
            Creator creator = new Creator();
            creator.setKey(key);
            creator.setValue(iterable);
            creator.setCondition(OptionalConstant.AND);
            creator.setCrucialState(OptionalConstant.NOT_IN);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls orEqualTo(String key,Object value,String... alias){
        if(Objects.nonNull(value)) {
            Creator creator = new Creator();
            creator.setKey(key);
            creator.setValue(Collections.singletonList(value));
            creator.setCondition(OptionalConstant.OR);
            creator.setCrucialState(OptionalConstant.EQUAL);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls orNotEqualTo(String key,Object value,String... alias){
        if(Objects.nonNull(value)) {
            Creator creator = new Creator();
            creator.setKey(key);
            creator.setValue(Collections.singletonList(value));
            creator.setCondition(OptionalConstant.OR);
            creator.setCrucialState(OptionalConstant.NOT_EQUAL);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls orIn(String key,Iterable<?> iterable,String... alias){
        if(Objects.nonNull(iterable)) {
            Creator creator = new Creator();
            creator.setKey(key);
            creator.setValue(iterable);
            creator.setCondition(OptionalConstant.OR);
            creator.setCrucialState(OptionalConstant.IN);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls orNotIn(String key,Iterable<?> iterable,String... alias){
        if(Objects.nonNull(iterable)) {
            Creator creator = new Creator();
            creator.setKey(key);
            creator.setValue(iterable);
            creator.setCondition(OptionalConstant.OR);
            creator.setCrucialState(OptionalConstant.NOT_IN);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls andBetween(String key,Object value,Object value2,String... alias){
        if(Objects.nonNull(value) && Objects.nonNull(value2)) {
            Creator creator = new Creator();
            creator.setKey(key);
            List<Object> list = new ArrayList<>(2);
            list.add(value);
            list.add(value2);
            creator.setValue(list);
            creator.setCondition(OptionalConstant.AND);
            creator.setCrucialState(OptionalConstant.BETWEEN);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls andNotBetween(String key,Object value,Object value2,String... alias){
        if(Objects.nonNull(value) && Objects.nonNull(value2)) {
            Creator creator = new Creator();
            creator.setKey(key);
            List<Object> list = new ArrayList<>(2);
            list.add(value);
            list.add(value2);
            creator.setValue(list);
            creator.setCondition(OptionalConstant.AND);
            creator.setCrucialState(OptionalConstant.NOT_BETWEEN);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }
    public Sqls orBetween(String key,Object value,Object value2,String... alias){
        if(Objects.nonNull(value) && Objects.nonNull(value2)) {
            Creator creator = new Creator();
            creator.setKey(key);
            List<Object> list = new ArrayList<>(2);
            list.add(value);
            list.add(value2);
            creator.setValue(list);
            creator.setCondition(OptionalConstant.OR);
            creator.setCrucialState(OptionalConstant.BETWEEN);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls orNotBetween(String key,Object value,Object value2,String... alias){
        if(Objects.nonNull(value) && Objects.nonNull(value2)) {
            Creator creator = new Creator();
            creator.setKey(key);
            List<Object> list = new ArrayList<>(2);
            list.add(value);
            list.add(value2);
            creator.setValue(list);
            creator.setCondition(OptionalConstant.OR);
            creator.setCrucialState(OptionalConstant.NOT_BETWEEN);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls andMoreThanEqualTo(String key,Object value,String... alias){
        if(Objects.nonNull(value)) {
            Creator creator = new Creator();
            creator.setKey(key);
            creator.setValue(Collections.singletonList(value));
            creator.setCondition(OptionalConstant.AND);
            creator.setCrucialState(OptionalConstant.MORE_THAN_EQUAL);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls andLessThanEqualTo(String key,Object value,String... alias){
        if(Objects.nonNull(value)) {
            Creator creator = new Creator();
            creator.setKey(key);
            creator.setValue(Collections.singletonList(value));
            creator.setCondition(OptionalConstant.AND);
            creator.setCrucialState(OptionalConstant.LESS_THAN_EQUAL);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls orMoreThanEqualTo(String key,Object value,String... alias){
        if(Objects.nonNull(value)) {
            Creator creator = new Creator();
            creator.setKey(key);
            creator.setValue(Collections.singletonList(value));
            creator.setCondition(OptionalConstant.OR);
            creator.setCrucialState(OptionalConstant.MORE_THAN_EQUAL);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }

    public Sqls orLessThanEqualTo(String key,Object value,String... alias){
        if(Objects.nonNull(value)) {
            Creator creator = new Creator();
            creator.setKey(key);
            creator.setValue(Collections.singletonList(value));
            creator.setCondition(OptionalConstant.OR);
            creator.setCrucialState(OptionalConstant.LESS_THAN_EQUAL);
            if(alias.length!=0){
                creator.setAlias(alias[0]);
            }
            this.sqls.add(creator);
        }
        return this;
    }
    public List<Creator> getSqls() {
        return sqls;
    }

    public void setSqls(List<Creator> sqls) {
        this.sqls = sqls;
    }

    public CreatorSort getCreatorSort() {
        return creatorSort;
    }

    public void setCreatorSort(CreatorSort creatorSort) {
        this.creatorSort = creatorSort;
    }

    public static class Creator{
        private String condition;
        private String crucialState;
        private String key;
        private Iterable<?> value;
        private String alias;

        public String getAlias() {
            return alias;
        }

        public void setAlias(String alias) {
            this.alias = alias;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getCondition() {
            return condition;
        }

        public void setCondition(String condition) {
            this.condition = condition;
        }

        public String getCrucialState() {
            return crucialState;
        }

        public void setCrucialState(String crucialState) {
            this.crucialState = crucialState;
        }

        public Iterable<?> getValue() {
            return value;
        }

        public void setValue(Iterable<?> value) {
            this.value = value;
        }
    }

    public static class CreatorSort{
        private Iterable<String> keys;
        private SortEnum condition;
        private String alias;

        public Iterable<String> getKeys() {
            return keys;
        }

        public void setKeys(Iterable<String> keys) {
            this.keys = keys;
        }

        public SortEnum getCondition() {
            return condition;
        }

        public void setCondition(SortEnum condition) {
            this.condition = condition;
        }

        public String getAlias() {
            return alias;
        }

        public void setAlias(String alias) {
            this.alias = alias;
        }
    }

}
